﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChineseSchool.Models.Hethong;
using System.Data.SqlClient;
using System.Data;
using ChineseSchool.Models;

namespace ChineseSchool.BLL
{
    public class B_Activities
    {
        private PetaPoco.Database db;
        public B_Activities()
        {
            db = new PetaPoco.Database("ConnectionString");
        }

        public List<M_Activities> GetAlls(int Count)
        {
            var Activities = db.Fetch<M_Activities>(@";EXEC Activities_Get_List @@ResultCount = @0", Count);
            return Activities;
        }

        public ObjectWithPaging<M_Activities> GetAllWithPaging(int pageIndex, int pageSize)
        {
            var result = db.Page<M_Activities>(pageIndex, pageSize, "SELECT * FROM Activities");

            ObjectWithPaging<M_Activities> activities = new ObjectWithPaging<M_Activities>();
            
                activities.CurrentPage= result.CurrentPage;
                
                activities.TotalPages = result.TotalPages ;
                activities.TotalItems = result.TotalItems ;
                activities.Items = result.Items;

            return activities;
        }

        public M_Activities GetByPK(int ActivitiesID)
        {
            var loadActivities = db.SingleOrDefault<M_Activities>("SELECT * FROM Activities m WHERE m.ActivitiesID = " + ActivitiesID + ";");
            db.CloseSharedConnection();
            return loadActivities;
        }

        public List<M_Activities> GetLastestActivities()
        {
            var strSql = @"SELECT TOP 10 ActivitiesID, ActivitiesTitle, ActivitiesDate
                            FROM Activities
                            ORDER BY ActivitiesDate DESC";
            var lstTopUser = db.Fetch<M_Activities>(strSql);
            return lstTopUser;
        }
    }
}